package com.jiaai.web.xiaoke.trade;

import com.alibaba.dubbo.config.annotation.Reference;
import com.jiaai.auth.model.AccessUser;
import com.jiaai.exception.ServerException;
import com.jiaai.family.model.TmemberVO;
import com.jiaai.trade.api.TTradeApi;
import com.jiaai.trade.model.TTradeAnchorIdVO;
import com.jiaai.trade.model.TTradeVO;
import com.jiaai.util.RequestUtils;
import com.jiaai.util.RestResultGenerator;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * TODO
 * </p>
 *
 * @author 佳爱网络科技
 * @version V1.0
 * @title appTradeController
 * @since 2017/12/27
 */
@RestController
@RequestMapping("/api/v1/app/trade")
public class appTradeController {
    @Reference
    private TTradeApi tradeApi;
    /**
     * 获取所有的公会列表带人数统计
     * @param curr
     * @param tTradeVO
     * @return
     * @throws Exception
     */
    @GetMapping("/find")
    @ResponseBody
    public Object find(@RequestParam(defaultValue = "0", required = false) Integer curr,TTradeVO tTradeVO ) throws Exception {
        return RestResultGenerator.genSuccessResult(tradeApi.find(curr, tTradeVO));
    }
    /**
     * 获取所有的主播
     * @param curr
     * @param tmemberVO
     * @return
     * @throws Exception
     */
    @GetMapping("/allauthor")
    @ResponseBody
    public Object allauthor(@RequestParam(defaultValue = "0", required = false) Integer curr, TmemberVO tmemberVO ) throws Exception {
        return RestResultGenerator.genSuccessResult(tradeApi.allauthor(curr, tmemberVO));
    }

    /**
     * 获取单个公会
     * @param tradeId
     * @return
     * @throws Exception
     */
    @PostMapping("/findById")
    public Object findById(Integer tradeId) throws ServerException {
        try {
            return RestResultGenerator.genSuccessResult(tradeApi.findById(tradeId));
        } catch (Exception e) {
            throw new ServerException("查询失敗");
        }
    }
    /**
     * 删除单个公会
     * @param tradeId
     * @return
     * @throws Exception
     */
    @PostMapping("/delete")
    public Object delete(Integer tradeId) throws ServerException {
        AccessUser accessUser = RequestUtils.getUser();
        try {
           if(((TTradeVO)tradeApi.findById(tradeId)).getmId().equals(accessUser.getUserId())) {
               tradeApi.delete(tradeId);
               return RestResultGenerator.genSuccessResult("删除成功");
            } else {
               throw new ServerException("暂无权限");
           }
        } catch (Exception e) {
            throw new ServerException("删除失败");
        }
    }
    /**
     * 添加公会
     * @param tTradeVO
     * @return
     * @throws Exception
     */
    @PostMapping("/addTrade")
    public Object addTrade(TTradeVO tTradeVO) throws ServerException {
        AccessUser accessUser = RequestUtils.getUser();
        try {
            tTradeVO.setmId(accessUser.getUserId());
            tradeApi.addTrade(tTradeVO);
            return RestResultGenerator.genSuccessResult("添加成功");
        } catch (Exception e) {
            throw new ServerException("添加失败");
        }
    }
    @PostMapping("/update")
    public Object update(TTradeVO tTradeVO) throws ServerException {
        AccessUser accessUser = RequestUtils.getUser();
        try {
//            ((TFamilyVO)tradeApi.findById(tTradeVO.getTradeId())).getmId().equals(accessUser.getUserId())
            if(1==1)  {
                tradeApi.update(tTradeVO);
                return RestResultGenerator.genSuccessResult("修改成功");
            } else {
                return "暂无权限修改";
            }
        } catch (Exception e) {
            throw new ServerException("修改失败");
        }
    }

    /**
     * 获取单个公会下面的所有成员
     * @param curr
     * @param tTradeVO
     * @return
     * @throws Exception
     */
    @GetMapping("/findByAllRole")
    @ResponseBody
    public Object findByAllRole(@RequestParam(defaultValue = "0", required = false) Integer curr, TTradeVO tTradeVO ) throws Exception {
        return RestResultGenerator.genSuccessResult(tradeApi.findByAllRole(curr, tTradeVO));
    }

    /**
     * 获取单个公会下面的所有主播直播信息
     * @param curr
     * @param tTradeAnchorIdVO
     * @return
     * @throws Exception
     */
    @GetMapping("/findByAllAnchorId")
    @ResponseBody
    public Object findByAllAnchorId(@RequestParam(defaultValue = "0", required = false) Integer curr, TTradeAnchorIdVO tTradeAnchorIdVO ) throws Exception {
        return RestResultGenerator.genSuccessResult(tradeApi.findByAllAnchorId(curr, tTradeAnchorIdVO));
    }

    /**
     * 统计单个公会下的所有人数
     * @param tradeId
     * @return
     * @throws Exception
     */
    @GetMapping("/countNum")
    public Object countNum(Integer tradeId) throws ServerException {
        try {
            return RestResultGenerator.genSuccessResult(tradeApi.countNum(tradeId));
        } catch (Exception e) {
            throw new ServerException("查询失敗");
        }
    }

}
